package com.elong.businesstravel.modules.home.ticket.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;

import com.android.support.jhf.utils.ToolUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.City;
import com.elong.businesstravel.entitys.TrainStation;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

/**
 * 城市列表自动完成
 * 
 * @author lenovo
 * 
 */
public class TrainStationAutoCompleteAdapter extends BaseAdapter implements Filterable {

	private Context mContext;
	private List<TrainStation> mTrainStations;
	private List<TrainStation> mRawTrainStations;
	private Filter mFilter;

	public TrainStationAutoCompleteAdapter(Context context, List<TrainStation> trainStations, List<TrainStation> rawTrainStations) {
		mContext = context;
		mTrainStations = trainStations;
		mRawTrainStations = rawTrainStations;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	public TrainStation getTrainStationByIndex(int index) {
		return mTrainStations.get(index);
	}

	/**
	 * Returns the context associated with this array adapter. The context is
	 * used to create views from the resource passed to the constructor.
	 * 
	 * @return The Context associated with this adapter.
	 */
	public Context getContext() {
		return mContext;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getCount() {
		return mTrainStations.size();
	}

	/**
	 * {@inheritDoc}
	 */
	public Object getItem(int position) {
		return mTrainStations.get(position).toString();
	}

	/**
	 * {@inheritDoc}
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * {@inheritDoc}
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.view_simple_item, null);
		}

		TextView itemTextView = (TextView) convertView
				.findViewById(R.id.itemTextView);
		itemTextView.setText(mTrainStations.get(position).getStationName());

		return convertView;
	}

	/**
	 * {@inheritDoc}
	 */
	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new StationArrayFilter(this, mRawTrainStations, mTrainStations);
		}
		return mFilter;
	}

	/**
	 * <p>
	 * An array filter constrains the content of the array adapter with a
	 * prefix. Each item that does not start with the supplied prefix is removed
	 * from the list.
	 * </p>
	 */
	private static class StationArrayFilter extends Filter {
		
		private List<TrainStation> mRawTrainStation;
		private List<TrainStation> mTrainStation;
		private TrainStationAutoCompleteAdapter mTrainStationAutoCompleteAdapter;

		public StationArrayFilter(TrainStationAutoCompleteAdapter cityAutoCompleteAdapter,
				List<TrainStation> rawTrainStation, List<TrainStation> trainStation) {
			mRawTrainStation = rawTrainStation;
			mTrainStation = trainStation;
			mTrainStationAutoCompleteAdapter = cityAutoCompleteAdapter;
		}

		/**
		 * The method is called when receive softkeybord entry's char
		 */
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {

			FilterResults filterResults = new FilterResults();

			List<TrainStation> tempCities = new ArrayList<TrainStation>();
			if (null != prefix) {
				if (!TextUtils.isEmpty(prefix.toString())) {
					for (int i = 0; i < mRawTrainStation.size(); i++) {
						if (mRawTrainStation.get(i).getStationName().startsWith(prefix
								.toString())) {
							tempCities.add(mRawTrainStation.get(i));
						} else if (mRawTrainStation.get(i).getStationPY().startsWith(prefix
								.toString().toLowerCase())) {
							tempCities.add(mRawTrainStation.get(i));
						}else if (mRawTrainStation.get(i).getStationPYS().startsWith(prefix
								.toString().toLowerCase())) {
							tempCities.add(mRawTrainStation.get(i));
						}
					}
				}
			}

			filterResults.values = tempCities;
			filterResults.count = tempCities.size();

			return filterResults;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			mTrainStation.clear();
			List<TrainStation> valuesCities = (List<TrainStation>) results.values;
			if (null != valuesCities) {
				mTrainStation.addAll(valuesCities);
			}
			if (results.count > 0) {
				mTrainStationAutoCompleteAdapter.notifyDataSetChanged();
			} else {
				mTrainStationAutoCompleteAdapter.notifyDataSetInvalidated();
			}
		}
	}
}
